//
//  MySettingsController.m
//  EcmDoctor
//
//  Created by 张进 on 15/7/14.
//  Copyright (c) 2015年 张进. All rights reserved.
//

#import "MySettingsController.h"
#import "AppUtils.h"
#import "ChangeDoctorState.h"
@interface MySettingsController ()
{
    __weak IBOutlet UIButton *_questionOnline;
    
    __weak IBOutlet UIButton *_changedFCN;
    __weak IBOutlet UIButton *_changedClinic;
    __weak IBOutlet UIButton *_questionTel;
    NSUserDefaults * _doctorUse;
}

@end

@implementation MySettingsController

//在线问诊
-(IBAction)switchValueChangedQuestionOnline:(UIButton*)sender{
    
    [self changeBtnStateWith:sender and:APP_ONLINESTATE_KEY];
    }
//电话问诊
-(IBAction)switchValueChangedQuestionTel:(UIButton*)sender{
  [self changeBtnStateWith:sender and:APP_PHONESTATE_KEY];
}
//门诊加号
-(IBAction)switchValueChangedClinic:(UIButton*)sender{
    [self changeBtnStateWith:sender and:APP_ADDNUMSTATE_KEY];
 }
//义诊消息推送
-(IBAction)switchValueChangedFCN:(UIButton*)sender{
    [self changeBtnStateWith:sender and:APP_FREESTATE_KEY];
   
}

- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = NO;
    
}
-(void)changeBtnStateWith:(UIButton *)sender and:(NSString *)key
{
    if(sender.selected)
    {
        [_doctorUse setObject:@"0" forKey:key];
        [sender setImage:[UIImage imageNamed:@"未勾选"] forState:UIControlStateNormal];
        sender.selected=NO;
    }
    else
    {
        [sender setImage:[UIImage imageNamed:@"勾选"] forState:UIControlStateNormal];
        [_doctorUse setObject:@"1" forKey:key];
        sender.selected=YES;
    }
    [_doctorUse synchronize];

}
- (IBAction)_logOut:(id)sender {
    [_doctorUse setObject:nil forKey:APP_DOCTORICON_KEY];
    [_doctorUse setObject:@"yizhuxiao" forKey:APP_KEY];
    [_doctorUse synchronize];
    [SSKeychain deletePasswordForService:APP_SERVICE_NAME account:APP_PHONENUM_KEY];
    [SSKeychain deletePasswordForService:APP_SERVICE_NAME account:APP_PASSWORD_KEY];
    [SSKeychain deletePasswordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY];
    [SSKeychain deletePasswordForService:APP_SERVICE_NAME account:APP_DOCTORHXID_KEY];
    [SSKeychain deletePasswordForService:APP_SERVICE_NAME account:APP_DOCTORHXPASSWORD_KEY];
    [SSKeychain deletePasswordForService:[AppUtils appServiceName] account:[AppUtils tokenKey]];
    [self performSegueWithIdentifier:@"logOut"sender:self];

}
-(void)viewWillDisappear:(BOOL)animated
{
    [ChangeDoctorState changeDoctorStateWithDoctorId: [SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY] Fs:[_doctorUse objectForKey:APP_FREESTATE_KEY] Ols:[_doctorUse objectForKey:APP_ONLINESTATE_KEY] Phs:[_doctorUse objectForKey:APP_PHONESTATE_KEY] Adds:[_doctorUse objectForKey:APP_ADDNUMSTATE_KEY] success:^(ChangeDoctorState *changeDoctorState) {
        NSLog(@"---------%@-------",changeDoctorState.code);
    } failure:^(NSError *error) {
        NSLog(@"---------%@",error.localizedDescription);
    }];

    
}
-(void)changeToNavRootController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
        
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _doctorUse=[NSUserDefaults standardUserDefaults];
    self.view.backgroundColor = [AppUtils getBackground];
    self.navigationItem.titleView=[AppUtils generateNavigationItemTitleViewWithTitle:@"设置"];
    //设置指示BTN初始状态
    [self setBtnStateWith:_questionOnline And:APP_ONLINESTATE_KEY];
    [self setBtnStateWith:_questionTel And:APP_PHONESTATE_KEY];
    [self setBtnStateWith:_changedClinic And:APP_ADDNUMSTATE_KEY];
    [self setBtnStateWith:_changedFCN And:APP_FREESTATE_KEY];
       //左上角返回按钮
    UIImage *tableIcon = [UIImage imageNamed:@"btn_back"];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithImage:tableIcon style:UIBarButtonItemStylePlain target:self action:@selector(changeToNavRootController:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithPatternImage:tableIcon];
}
-(void)setBtnStateWith:(UIButton *)btn And:(NSString *)key
{
  
   
    NSString *state=[NSString stringWithFormat:@"%@",[_doctorUse objectForKey:key]];
    if([state isEqualToString:@"1"])
    {
        [btn setImage:[UIImage imageNamed:@"勾选"] forState:UIControlStateNormal];
        btn.selected=YES;
    }
    else
    {
        [btn setImage:[UIImage imageNamed:@"未勾选"] forState:UIControlStateNormal];
        btn.selected=NO;
    }

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        
        //需要注意的是self.isViewLoaded是必不可少的，其他方式访问视图会导致它加载 ，在WWDC视频也忽视这一点。
        
        if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图
        {
            // Add code to preserve data stored in the views that might be
            // needed later.
            
            // Add code to clean up other strong references to the view in
            // the view hierarchy.
            self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        }
        
    }
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 45.0)];
    view.backgroundColor = [UIColor clearColor];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(40, 10, 150, 30)];
    label.backgroundColor=[UIColor clearColor];
    if(section==0)
    label.text=@"我的服务设置";
    else
        label.text=@"推送消息设置";
    [view addSubview:label];
    
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
    
}


@end
